﻿#region Using Statemens
using System;
using System.Collections.Generic;
using GameBase.Screens;
using Microsoft.Xna.Framework;
using StarFox.ScreenComponents;
using StarFox.Sounds;
#endregion

namespace StarFox.Screens
{
    /// <summary>
    /// Sound options menu screen of the game.
    /// </summary>
    public class SoundOptionsScreen : StarFoxScreen
    {
        #region Fields

        // game settings management...
        // ... sound effects volume level (0 - 10)
        // ... music volume level (0 - 10)
        GameSettings gameSettings;

        GamePlaySound gamePlaySound;

        ApplicationMenu menu;

        #endregion

        #region Initialization

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screenManager">The ScreenManager that this Screen is associated to.</param>
        public SoundOptionsScreen(ScreenManager screenManager)
            : base(screenManager)
        {
            Name = "SoundOptionsScreen";
        }

        /// <summary>
        /// Called when the Screen needs to be initialized.
        /// </summary>
        public override void Initialize()
        {
            gameSettings = Game.Services.GetService(typeof(GameSettings)) as GameSettings;
            if (gameSettings == null)
                throw new InvalidOperationException("Service of type GameSettings not found");

            gamePlaySound = Game.Services.GetService(typeof(GamePlaySound)) as GamePlaySound;
            if (gamePlaySound == null)
                throw new InvalidOperationException("Service of type StarFoxSound not found");

            Components3D.Add(new MenuBackground(this) { Duration = TimeSpan.FromSeconds(6) });

            menu = new ApplicationMenu(this);
            // menu title
            menu.MenuTitle = "TITLE_SOUND_OPTIONS";
            // menu items
            List<ApplicationMenu.IMenuItem> auxMenuItems = new List<ApplicationMenu.IMenuItem>();
            auxMenuItems.Add(new ApplicationMenu.BarItem("ITEM_EFFECTS_VOLUME", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.BACK, ApplicationMenu.ButtonMessage.DEFAULTS }, gameSettings.SfxVolLevel, gameSettings.DefSfxVolLevel, 10, 0));
            auxMenuItems.Add(new ApplicationMenu.BarItem("ITEM_MUSIC_VOLUME", new ApplicationMenu.ButtonMessage[] { ApplicationMenu.ButtonMessage.BACK, ApplicationMenu.ButtonMessage.DEFAULTS }, gameSettings.MusVolLevel, gameSettings.DefMusVolLevel, 10, 0));
            menu.MenuItems = auxMenuItems;
            // menu actions
            menu.MenuScroll += (sender, args) =>
            {
                switch (args.Index)
                {
                    case 0:
                        // sound effects volume level
                        gameSettings.SfxVolLevel = ((ApplicationMenu.BarItem)menu.MenuItems[args.Index.Value]).CurrentBarLevel;
                        ScreenSound.SetVolume((float)gameSettings.SfxVolLevel / 10.0f);
                        gamePlaySound.SetVolume((float)gameSettings.SfxVolLevel / 10.0f);
                        break;
                    case 1:
                        // music volume level
                        gameSettings.MusVolLevel = ((ApplicationMenu.BarItem)menu.MenuItems[args.Index.Value]).CurrentBarLevel;
                        PlayList.SetVolume((float)gameSettings.MusVolLevel / 10.0f);
                        break;
                    default:
                        break;
                }
            };
            menu.MenuDefault += (sender, args) =>
            {
                // default values of menu items                        
                gameSettings.DefaultSoundSettings();
                ScreenSound.SetVolume((float)gameSettings.SfxVolLevel / 10.0f);
                PlayList.SetVolume((float)gameSettings.MusVolLevel / 10.0f);
            };

            Components2D.Add(menu);

            base.Initialize();
        }

        #endregion
    }
}